/**
 * @description Description
 *
 * @author Candy <candy.yan@motionglobal.com>
 * @date  Creation Date 2018/8/15
 */
function caleSliderViewNum() {
	var sliderViewNum, tid;
	var bodyWidth = parseInt($('body').width());
	$('.swiper-scrollbar-cover').width(0);
	$('.slider-bar-wrapper .swiper-scrollbar-drag').css({'background-color': '#ccc'});
	switch (true) {
		case parseInt(bodyWidth) >= 1224:
			sliderViewNum = 4;
			break;
		case parseInt(bodyWidth) > 812 && parseInt(bodyWidth) <= 1223:
			sliderViewNum = 3;
			break;
		case parseInt(bodyWidth) <= 812:
			sliderViewNum = 2;
			break;
		default:
			sliderViewNum = 4;
			break;
	}
	var swiper = new Swiper('#product-recommend-slide', {
		navigation: {
			nextEl: '#product-recommend-slide .swiper-button-next',
			prevEl: '#product-recommend-slide .swiper-button-prev',
		},
		watchSlidesProgress: true,
		watchSlidesVisibility : true,
		slidesPerView : sliderViewNum,
		scrollbar: {
			el: '#product-recommend-slide .swiper-scrollbar',
			dragSize: 30,
			draggable: true,
		},
		on:{
			slideChange: function () {
				var swiperScrollbarCover = $('#product-recommend-slide .swiper-scrollbar-cover');
				var sliderWrapperWidth = $('#product-recommend-slide .slider-bar-wrapper').width();
				var sliderBlockNum = $('#product-recommend-slide .swiper-slide').length - sliderViewNum;
				swiperScrollbarCover.css({'width': (sliderWrapperWidth / sliderBlockNum) * this.activeIndex + 'px'});
				if (this.activeIndex == 0) {
					$('#product-recommend-slide .swiper-scrollbar-drag').css({'background-color': '#ccc'})
				} else {
					$('#product-recommend-slide .swiper-scrollbar-drag').css({'background-color': '#50b0ac'})
				}
			},
		}
	});
	// Calculation product image wrapper height
	var proImgListHeight = $('.pro-img-list').height();
	$('.product-img').height(proImgListHeight);
}
(function(win) {
	caleSliderViewNum();
	win.addEventListener('resize', function() {
		clearTimeout(tid);
		tid = setTimeout(caleSliderViewNum, 300);
	}, false);
	win.addEventListener('pageshow', function(e) {
		if (e.persisted) {
			clearTimeout(tid);
			tid = setTimeout(caleSliderViewNum, 300);
		}
	}, false);
})(window);

(function ($) {
	//Product image popup
	$('.pro-img-list li').click( function () {
		$(this).find('.pro-img-popup').fadeIn();
	});
	$('.pro-img-list li .popup-close-btn').click(function (e) {
		e.stopPropagation();
		$(this).parents('.pro-img-popup').fadeOut();
	});
	//Review image popup
	$('.article-img-list .review-img .zoom-icon').click( function () {
		$(this).siblings('.article-img-popup').fadeIn();
	});
	$('.article-img-popup .popup-close-btn').click(function (e) {
		e.stopPropagation();
		$(this).parents('.article-img-popup').fadeOut();
	});

	// product color tab
	$('.pro-color .pro-thumbs li').click(function () {
		if ($(this).hasClass('selected')) {
			return;
		}
		var curSelectColor = $(this).data('selected-color');
		$(this).addClass('selected');
		$(this).siblings().removeClass('selected');
		$('.pro-img-list').fadeOut();
		$('#product-img-' + curSelectColor).fadeIn();
	});
	// product strength tab
	$('.pro-nicotine-strength .pro-thumbs li').click(function () {
		if ($(this).hasClass('selected')) {
			return;
		}
		var curSelectStrength = $(this).data('selected-strength');
		$(this).addClass('selected');
		$(this).siblings().removeClass('selected');
		$('.pro-img-list').fadeOut();
		$('#product-strength-' + curSelectStrength).fadeIn();
	});

	//product add ons checkbox
	$('.pro-add-ons .checkbox-item').click(function () {
		$(this).find('input').prop('checked', true);
		$(this).siblings().find('input').prop('checked', false);
	});

	// Product description toggle
	$('.description-title').click(function () {
		var curToggle = $(this).siblings('.description-cnt').attr('toggle');
		$(this).parent().siblings().find('.description-cnt').attr('toggle', 'hide');
		$(this).parent().siblings().find('.description-cnt').slideUp();
		$(this).parent().siblings().find('.description-title .toggle-icon').text('+');
		if (curToggle == 'hide') {
			$(this).find('.toggle-icon').text('-');
			$(this).siblings('.description-cnt').slideDown();
			$(this).siblings('.description-cnt').attr('toggle', 'show');
		} else if (curToggle == 'show') {
			$(this).find('.toggle-icon').text('+');
			$(this).siblings('.description-cnt').slideUp();
			$(this).siblings('.description-cnt').attr('toggle', 'hide');
		}
	});

	//Product review tab
	$('.product-others-nav a').click(function () {
		var tabIndex = $(this).data('index');
		$(this).siblings().removeClass('current');
		$(this).addClass('current');
		$('#tab-' + tabIndex).siblings('.product-others-cnt').hide();
		$('#tab-' + tabIndex).fadeIn();
	});

	$('.write-a-review').click(function () {
		var tabIndex = $(this).data('index');
		$(this).hide();
		$(this).siblings('.review-form-close').show();
		$('#tab-' + tabIndex).slideDown();
	});
	$('.review-form-close').click(function () {
		var tabIndex = $(this).data('index');
		$(this).hide();
		$(this).siblings('.write-a-review').show();
		$('#tab-' + tabIndex).slideUp();
	});
	//Write a review select stars
	$('.form-star-select a').click(function () {
		var starsOption = $(this).attr('option');
		if (starsOption == 1 && $(this).hasClass('cur')) {
			$(this).parent().siblings('.form-star-input').val('');
			selectStart(0);
			return;
		}
		$(this).parent().siblings('.form-star-input').val(starsOption);
		selectStart(parseInt(starsOption));
	});
	function selectStart(selectedStarsLen) {
		$('.form-star-select a').map(function (index, item) {
			if (selectedStarsLen > index) {
				$(item).addClass('cur');
			} else {
				$(item).removeClass('cur');
			}
		})
	}
	//Review form validate
	$("#write-review-form").validate({
		rules : {
			name : {
				required : true,
			},
			title : {
				required : true,
			},
			review : {
				required : true,
			},
		},
		messages : {
			name : {
				required : 'Please enter name.',
			},
			title : {
				required : 'Please enter title.',
			},
			review : {
				required : 'Please enter review content.',
			},
		}
	});
})(jQuery);